package com.definesys.smecrailtransit.base;
import android.content.Context;

import com.definesys.smecrailtransit.SmecApplication;
import com.definesys.smecrailtransit.SmecManager.SmecRealm.RealmManager;
import com.definesys.smecrailtransit.SmecManager.SmecRetrofit.ApiException;
import com.definesys.smecrailtransit.SmecManager.SmecRetrofit.RetrofitServiceManager;
import com.definesys.smecrailtransit.api.SmecRailService;
import com.definesys.smecrailtransit.utils.NetworkState;
import com.hwangjr.rxbus.Bus;
import com.hwangjr.rxbus.SmecRxBus;

import java.io.Serializable;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.schedulers.Schedulers;

/**
 * Created by xupeizuo on 2017/7/20.
 */

public class SmecBasePresenter implements Serializable{

    protected Context mContext;
    protected Bus mRxbus;
    protected SmecRailService smecRailService;
    protected RealmManager realmManager;

    public SmecBasePresenter(Context mContext) {
        this.mContext = mContext;
        mRxbus= SmecRxBus.get();
        mRxbus.register(this);
        realmManager=RealmManager.getInstance();
        smecRailService= RetrofitServiceManager.getSmecRailService();
    }

    /**
     * 调度线程，绑定生命周期，判断有无网络连接
     * @param observable
     * @param <T>
     * @return
     */
    protected <T>Observable<T>schedulerThread(Observable<T> observable){
        return observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {
                        if(!NetworkState.networkConnected(SmecApplication.getContext())){
                            throw new ApiException("","无网络连接");
                        }
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread());
    }

}
